<template>
	<view class="mainpadding beijingse">
		<view class="" style="padding-bottom: 130rpx;">
			<view class="ershil xiaohei textright" v-if="gouwcnr.length" @click="guanliflag=!guanliflag">
				{{guanliflag?"管理":"完成"}}</view>
			<view class="mainpadding ffffff radius margin_top" v-for="item in gouwcnr" :key="item.goods_id"
				@click="selectitem(item)">
				<view class="flexbetween">
					<view class="weixz margin_right2" v-if="!item.is_checked"></view>
					<view class="margin_right2" v-if="item.is_checked">
						<u-icon name="checkmark-circle-fill" color="#FF9341" size="24"></u-icon>
					</view>
					<view class="flexbetween" style="width: 90%;">
						<image class="shpt " :src="item.image" mode=""></image>
						<view class="" style="width: 62%;">
							<view class="titletext xiaohei">{{item.title}}</view>
							<view class="strongtext xiaohei margin_top yhxk">{{item.sku.difference}}</view>
							<view class="flexbetween margin_top">
								<view class="titletext xiaohuang">¥{{item.sku.price}}</view>
								<u-number-box button-size="36" @change="changenum($event,item)" :asyncChange="true"
									color="#000000" bgColor="#F7F7F7" iconStyle="color: #fff"
									v-model="item.number"></u-number-box>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 固定底部 -->
		<view class="gudingdb ffffff mainpadding flexbetween" v-if="gouwcnr.length">
			<view class="flexleft" @click="xiaoshi()">
				<view class="weixz margin_right2" v-if="!quan"></view>
				<view class="margin_right2" v-if="quan">
					<u-icon name="checkmark-circle-fill" color="#FF9341" size="24"></u-icon>
				</view>
				<view class="titletext xiaohei">全选</view>
			</view>
			<view class="libtn flexcenter" @click="buy" v-if="guanliflag">立即支付</view>
			<view class="libtn flexcenter" style="background-color: #ff0000;" @click="scgwc" v-else>移出购物车</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				quan: false,
				gouwcnr: [], //购物车列表内容
				allprice: 0,
				guanliflag: true,
				is_login:false
			}
		},
		onShow() {
			this.is_login = httpRequest.checkIsLogin();
			if (this.is_login) {
				this.gwclb()
			} else {
				this.gouwcnr = []
			}
		},
		onShareAppMessage: function(options) {
			return {
				title: '宜品佳商城',
				imageUrl: '',
				path: '/pages/homepage?otherinvite_code='+uni.getStorageSync("invite_code") || ""
			}
		},
		onShareTimeline: function() {
			return {
				title: '宜品佳商城',
				imageUrl: '',
				path: '/pages/homepage?otherinvite_code='+uni.getStorageSync("invite_code") || ""
			}
		},
		watch: {
			gouwcnr: {
				handler(newValue, oldValue) {
					let isall_check = 0
					let allprice = 0
					newValue.forEach(item => {
						if (item.is_checked) {
							allprice = allprice + Number(item.price) * Number(item.number)
							isall_check = isall_check + 1
						}
					})
					this.allprice = allprice
					if (isall_check == this.gouwcnr.length) {
						this.quan = true
					} else {
						this.quan = false
					}
				},
				deep: true
			}
		},
		methods: {
			changetab(e) {
				this.data_type = e.value
				this.gouwcnr = []
				this.gwclb()
			},
			buy() {
				let data = {
					type: "cart", //detail=商品详情,cart=购物车
					order_type:2,
					data: []
				}
				let num = 0
				this.gouwcnr.forEach(item => {
					if (item.is_checked) {
						data.data.push({
							goods_id: item.goods_id,
							number: item.number,
							sku_id: item.sku_id,
						})
						num = num + 1
					}
				})
				if (num == 0) {
					httpRequest.toast("请选择商品")
					return
				}
				uni.navigateTo({
					url: '/pages_homepage/querendd?msg=' + encodeURIComponent(JSON.stringify(data))
				})
			},
			// 跳转商品详情
			goGoodsInfo(id) {
				uni.navigateTo({
					url: '/pages_homepage/shangpxq?id=' + id
				})
			},
			// 删除购物车
			scgwc() {
				let data = []
				this.gouwcnr.forEach(item => {
					if (item.is_checked) {
						data.push({
							goods_id: item.goods_id,
							sku_id:item.sku_id
						})
					}
				})
				httpRequest.request('/api/cart/delCart', 'POST', {
					data,
					data_type: this.data_type,
				}, false, false, true).then(res => {
					httpRequest.toast(res.msg)
					if (res.code == 1) {
						this.gwclb()
					}
				})
			},
			// 购物车列表接口
			gwclb() {
				httpRequest.request('/api/cart/cartList', 'POST', {
				}, false, false, true).then(res => {
					if (res.code == 1) {
						res.data.forEach(item => {
							this.$set(item, "is_checked", false)
						})
						this.gouwcnr = res.data
					}
				})
			},
			changenum(e, item) {
				item.number = e.value
				httpRequest.request('/api/cart/clCart', 'POST', {
					goods_id: item.goods_id,
					number: e.value,
					sku_id:item.sku_id
				}, false, false, true).then(res => {
					if (res.code != 1) {
						httpRequest.toast(res.msg)
					}
				})
			},
			selectitem(item) {
				console.log(item);
				item.is_checked = item.is_checked ? false : true
			},
			// 立即购买
			atonceBuy() {
				let data = {
					msg: [],
					type: "cart" //car购物车，detail详情
				}
				this.gouwcnr.forEach(item => {
					if (item.is_checked) {
						data.msg.push({
							goodsId: item.id,
							specType: 0,
							num: item.number
						})
					}
				})
				if (data.msg.length == 0) {
					return
				}
				uni.navigateTo({
					url: '/pages/gouwuche/querendd?msg=' + encodeURIComponent(JSON.stringify(data))
				})

			},
			xiaoshi() {
				if (this.quan) {
					this.quan = false
					this.gouwcnr.forEach(item => {
						item.is_checked = false
					})
				} else {
					this.quan = true
					this.gouwcnr.forEach(item => {
						item.is_checked = true
					})
				}

			}
		}
	}
</script>

<style lang="scss" scoped>
	.beijingse {
	    background: #F8F8F8;
		/* #ifdef APP-PLUS */
		min-height:calc(100vh - 100rpx);
		/* #endif */
		/* #ifdef MP-WEIXIN */
		min-height:calc(100vh);
		/* #endif */
		height: auto;
	}
	.libtn {
		font-size: 32rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
		width: 226rpx;
		height: 76rpx;
		background: #FF9341;
		border-radius: 120rpx 120rpx 120rpx 120rpx;
	}

	.jianbox {
		width: 178rpx;
		height: 48rpx;
		border-radius: 8rpx 8rpx 8rpx 8rpx;
		opacity: 1;
		border: 1rpx solid #D2D2D2;
	}

	.biak {
		width: 1rpx;
		height: 47rpx;
		background-color: #D2D2D2;
	}

	.shpt {
		width: 182rpx;
		height: 182rpx;
		border-radius: 14rpx 14rpx 14rpx 14rpx;
	}
</style>